package com.shakespace.artofandroid.chapter03.basic

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup

class EventDemoView @JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    ViewGroup(context, attrs, defStyleAttr) {

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {


    }

    /**
     *
    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
    var consume = false;
    if (onInterceptTouchEvent(ev)) {
    consume = onTouchEvent(ev)
    } else {
    consume = child.dispatchTouchEvent(ev)
    }
    return consume
    }
     *  1. begin with current ViewGroup
     *      1.2 call dispatchTouchEvent
     *          if onInterceptTouchEvent is true, will call onTouchEvent in this viewGroup
     *          if false , go to its child
     * 2. child  work like above
     * 3. in child, if onTouchEvent is true, finish
     * 4. if false , back to parent , if all false , do nothing for this event
     *
     *
     * if setOnTouchListener , will call OnTouchListener.onTouch() in  dispatchTouchEvent .
     *
     * OnClickListener will called in OnTouchEvent()
     */

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        var consume = false;
        if (onInterceptTouchEvent(ev)) {
            consume = onTouchEvent(ev)
        } else {
//            consume = child.dispatchTouchEvent(ev)
        }
//        return consume
        return super.dispatchTouchEvent(ev)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {

        return super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return super.onTouchEvent(event)
    }

    private val touchListener = object : View.OnTouchListener {
        override fun onTouch(v: View?, event: MotionEvent?): Boolean {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

    }
}